<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="Keywords" content="wild">
  <meta name="description" content="" />
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>

  <title>rule list</title>
  <link rel="stylesheet" type="text/css" href="/static/css/common.css">
  <link rel="stylesheet" type="text/css" href="/static/css/trade.css">
  <link rel="stylesheet" type="text/css" href="/static/js/lib/datatable/datatable.css" />

  <script type="text/javascript" src="/static/js/lib/jquery/3.2.1/jquery.js"></script>
  <script type="text/javascript" src="/static/js/lib/datatable/datatable.js"></script>
  <script type="text/javascript" src="/static/js/util/consts.js"></script>
  <script type="text/javascript" src="/static/js/util/config.js"></script>
  <script type="text/javascript" src="/static/js/util/common.js"></script>
  <script type="text/javascript" src="/static/js/util/frame.js"></script>
  <script type="text/javascript" src="/static/js/modules/tradeConfig.js"></script>

  <script type="text/javascript">

    pageConfiguration.needAuth = true;

    var dataTable = new DataTable();

    function onload() {
      renderTradeMenu(2);

      var table = $('#data-table-list');
      var index = 0;
      dataTable.init({
        src: table,
        dataTable: {
          ajax: {
            url: GlobalConfig.serverUrl + '/api/trade/ruleList',
            type: 'GET',
            data: {},
            headers: getRequestHeaders(),
            error: function(xhr) {
              ExceptionHandler.handleCommonError(xhr);
            }
          },
          dataSrc: 'data',
          columns: [
            {
              title: '序号',
              render: function(row) {
                return ++index;
              }
            },
            {
              title: '代码',
              render: function(row) {
                return row.stockCode;
              }
            },
            {
              title: '股票名称',
              render: function(row) {
                return row.stockName;
              }
            },
            {
              title: '规则',
              render: function(row) {
                return row.strategyName;
              }
            },
            {
              title: '类型',
              render: function(row) {
                return row.type;
              }
            },
            {
              title: '值',
              render: function(row) {
                return row.value;
              }
            },
            {
              title: '交易量',
              render: function(row) {
                return row.volume;
              }
            },
            {
              title: '初始价格',
              render: function(row) {
                return row.openPrice;
              }
            },
            {
              title: '最高价格',
              render: function(row) {
                return row.highestPrice;
              }
            },
            {
              title: '最低价格',
              render: function(row) {
                return row.lowestPrice;
              }
            },
            {
              title: '状态',
              render: function(row) {
                return row.state;
              }
            },
            {
              title: '最后更新时间',
              render: function(row) {
                return new Date(row.updateTime).format('yyyy-MM-dd hh:mm:ss');
              }
            },
            {
              title: '操作',
              render: function(row) {
                var html = '';
                if (row.state === 0) {
                  html += ' <button class="btn btn-validate" data-id="' + row.id + '">生效</button>';
                } else if (row.state === 1) {
                  html += ' <button class="btn btn-invalid" data-id="' + row.id + '">失效</button>';
                }
                html += ' <button class="btn btn-reset" data-id="' + row.id + '">重置交易</button>';
                return html;
              }
            }
          ],
          fnDrawCallback: function(result) {
            index = 0;
          }
        },
      });

      table.on('click', '.btn-validate', function() {
        var id = $(this).data('id');
        changeState(id, 1);
      });

      table.on('click', '.btn-invalid', function() {
        var id = $(this).data('id');
        changeState(id, 0);
      });

      table.on('click', '.btn-reset', function() {
        var id = $(this).data('id');
        $.ajax({
          url: GlobalConfig.serverUrl + '/api/trade/resetRule',
          type: 'POST',
          data: {
            id: id
          },
          headers: getRequestHeaders(),
          error: function(xhr, status, errorThrown) {
            ExceptionHandler.handleCommonError(xhr);
          },
          success: function(res) {
            alert(res.message);
            dataTable.fnDraw(1);
          }
        });
      });
    }

    function changeState(id, state) {
      $.ajax({
        url: GlobalConfig.serverUrl + '/api/trade/changeRuleState',
        type: 'POST',
        data: {
          id: id,
          state: state
        },
        headers: getRequestHeaders(),
        error: function(xhr, status, errorThrown) {
          ExceptionHandler.handleCommonError(xhr);
        },
        success: function(res) {
          alert(res.message);
          dataTable.fnDraw(1);
        }
      });
    }

  </script>

</head>

<body>
  <div id="head" class="head"></div>

  <div class="container">
    <div class="innerBox">
      <ul class="menu-nav">
      </ul>
      <table id="data-table-list" class="data-table-list">
      </table>
    </div>
  </div>

  <div id="foot" class="foot"></div>

</body>

</html>
